

#include "app_AppDispatch.h"
#include "src/loadcontainer.h"

CLoadContainer* pConatainer;

jstring str2jstring(JNIEnv* env,const char* pat)
{
    //定义java String类 strClass
    jclass strClass = (env)->FindClass("Ljava/lang/String;");
    //获取String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新String
    jmethodID ctorID = (env)->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
    //建立byte数组
    jbyteArray bytes = (env)->NewByteArray(strlen(pat));
    //将char* 转换为byte数组
    (env)->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
    // 设置String, 保存语言类型,用于byte数组转换至String时的参数
    jstring encoding = (env)->NewStringUTF("GB2312");
    //将byte数组转换为java String,并输出
    return (jstring)(env)->NewObject(strClass, ctorID, bytes, encoding);
}

std::string jstring2str(JNIEnv* env, jstring jstr)
{
    char*   rtn   =   NULL;
    jclass   clsstring   =   env->FindClass("java/lang/String");
    jstring   strencode   =   env->NewStringUTF("GB2312");
    jmethodID   mid   =   env->GetMethodID(clsstring,   "getBytes",   "(Ljava/lang/String;)[B");
    jbyteArray   barr=   (jbyteArray)env->CallObjectMethod(jstr,mid,strencode);
    jsize   alen   =   env->GetArrayLength(barr);
    jbyte*   ba   =   env->GetByteArrayElements(barr,JNI_FALSE);
    if(alen   >   0)
    {
        rtn   =   (char*)malloc(alen+1);
        memcpy(rtn,ba,alen);
        rtn[alen]=0;
    }
    env->ReleaseByteArrayElements(barr,ba,0);
    std::string stemp(rtn);
    free(rtn);
    return   stemp;
}

/*
 * Class:     com_example_snows_MainActivity
 * Method:    SendMessage
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_app_AppDispatch_SendMessage(JNIEnv* env, jclass obj, jstring msg)
{
    logi("Java_app_AppDispatch_SendMessage ==>%s","ok");

    std::string result = "ok";

    if (pConatainer != NULL) {

    }

    return env->NewStringUTF(result.c_str());
}

/*
 * Class:     com_example_snows_MainActivity
 * Method:    PostMessage
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT jstring JNICALL Java_app_AppDispatch_PostMessage(JNIEnv* env, jclass obj, jstring msg)
{
    logi("Java_app_AppDispatch_PostMessage ==>%s","ok");

    std::string result = "ok";

    if (pConatainer != NULL) {

    }

    return env->NewStringUTF(result.c_str());
}

/*
 * Class:     com_example_snows_MainActivity
 * Method:    RecvMessage
 * Signature: ()V
 */
JNIEXPORT jstring JNICALL Java_app_AppDispatch_RecvMessage(JNIEnv* env, jclass obj, jstring msg)
{

    logi("Java_app_AppDispatch_RecvMessage ==>%s","ok");

    std::string result = "ok";

    if (pConatainer != NULL) {

    }

    return env->NewStringUTF(result.c_str());
}
/*
 * Class:     com_example_snows_MainActivity
 * Method:    InitEnv
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_app_AppDispatch_InitEnv(JNIEnv* env, jclass obj, jstring key, jstring value,jint type, jint exit)
{
    std::string result = "ok";

    logi("Java_app_AppDispatch_InitEnv ==>%s","ok");

    pConatainer = ALLOC_NEW CLoadContainer();

    string path = jstring2str(env,value);
    const char* pCode = "{\"component\": [ 	\
        \"{F170A724-AACA-4603-BB1C-0A8EAF1C3322}:asynio:1:4\",	\
        \"{ED660EF7-7D0B-4CBE-8D92-D5F225434464}:http:2:4\",	\
        \"{C54C9CC0-F448-4A49-A622-0467D02E8EB8}:net:3:4\",	\
        \"{CE065148-7803-45B1-B1D7-874B52B8F5C5}:uibase:5:4\", \
        \"{201409F6-22F8-48d3-A69F-7935BDDE6BFF}:msgbus:4:4\"]}";


    logi("native run %s",path.c_str());
    path += "/";


    pConatainer->Init(NULL,path.c_str(),pCode);
    pConatainer->Start(NULL,0);
    return env->NewStringUTF(result.c_str());
}

/*
 * Class:     com_example_snows_MainActivity
 * Method:    UnInitEnv
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_app_AppDispatch_UnInitEnv(JNIEnv* env, jclass obj,jstring key, jstring value)
{
    logi("Java_app_AppDispatch_UnInitEnv ==>%s","ok");

    if (pConatainer) {

        pConatainer->Stop(NULL,0,0);
        pConatainer->UnInit(NULL);
        delete pConatainer;
    }

    std::string result = "ok";
    return env->NewStringUTF(result.c_str());
}
JNIEXPORT jstring JNICALL Java_app_AppDispatch_GetKey (JNIEnv* env, jclass obj,jstring key)
{
    logi("Java_app_AppDispatch_GetKey ==>%s","ok");

    std::string result = "ok";
    return env->NewStringUTF(result.c_str());
}


/*
 * Class:     app_AppDispatch
 * Method:    SetKey
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_app_AppDispatch_SetKey(JNIEnv* env, jclass obj, jstring key, jstring value)
{
    logi("Java_app_AppDispatch_SetKey ==>%s","ok");

    std::string result = "ok";
    return env->NewStringUTF(result.c_str());
}